package com.xsb.aspectj.aspect;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import com.xsb.aspectj.LoginActivity;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

/**
 * author : 薛少博
 * e-mail : xsb@zlhopesun.com
 * time   : 2021/6/15
 * desc   : 定义切面类
 */
@Aspect
public class LoginCheckAspect {
  private final static String TAG = "MainActivity";
  @Pointcut("execution(@com.xsb.aspectj.annotation.LoginCheck * *(..))")
  public void methodPointCut(){}
  @Around("methodPointCut()")
  public Object joinPoint(ProceedingJoinPoint joinPoint) throws Throwable{
    Context context = (Context) joinPoint.getThis();
    if(true){//这里判断是否登录值从shardPreference里面获取
      Log.e(TAG, "检测到以登录" );
      return joinPoint.proceed();
    }else {
      Log.e(TAG, "检测到未登录" );
      Toast.makeText(context,"请先登录！",Toast.LENGTH_SHORT).show();
      context.startActivity(new Intent(context, LoginActivity.class));
      //不执行方法，返回null
      return null;
    }
  }
}
